//
//  SHPVisitorView.swift
//  weibo
//
//  Created by songhuipan on 16/4/16.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit
import SnapKit

class SHPVisitorView: UIView {
    
    // 定义闭包
    var visitorCloseure :(() ->())?
    
    override init(frame:CGRect) {
        
        super.init(frame:frame)
        
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setVisitorInfo(imageName:String?,title:String? ) {
        
        if let img = imageName,tit = title {
            
            iconImageView.image = UIImage(named:img)
            
            messageLabel.text = tit
            
            // 隐藏圆圈
            circleImageView.hidden = true
            
        }else {
            
            setAnimationToCircleImageView()
        }
        
        
    }
    
    private func setUI (){
        backgroundColor = getColorWithRGB(237, green: 237, blue: 237)
    
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(logInButton)
        addSubview(registerButton)
        
        circleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
            
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(230, 36))
            make.centerX.equalTo(maskImageView)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 36))
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        logInButton.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 36))
            make.top.equalTo(registerButton)
        }
        
        
        
    }
    // 设置圆圈动画
    func setAnimationToCircleImageView (){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.duration = 15
        animation.repeatCount = MAXFLOAT
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        
        circleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    // 懒加载圆圈
    private lazy var circleImageView :UIImageView = {
        
        let circleImageView = UIImageView()
        
        circleImageView.image = UIImage(named:"visitordiscover_feed_image_smallicon")
        
        
        return circleImageView
        
    }()
    
    // 懒加载遮罩
    private lazy var maskImageView :UIImageView = {
        
        let maskImageView = UIImageView()
        
        maskImageView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return maskImageView
        
    }()
    // 懒加载小房子
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 懒加载文字
    private lazy var messageLabel :UILabel = {
        // 使用遍历构造函数创建label
        let messageLabel = UILabel(textColor: UIColor.grayColor(), textFont: 14)
        
        messageLabel.numberOfLines = 0
        
        messageLabel.textAlignment = .Center
        
        messageLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        return messageLabel
    }()
    
    // 懒加载登录按钮
    private lazy var logInButton :UIButton = {
        
        let logInButton = UIButton()
        
        logInButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        logInButton.setTitle("登录", forState: UIControlState.Normal)
        
        logInButton.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        
        logInButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        logInButton.addTarget(self, action: "logInButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return logInButton
    }()
    // 懒加载注册按钮
    private lazy var registerButton :UIButton = {
        let registerButton = UIButton()
        
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        registerButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        registerButton.addTarget(self, action: "logInButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
    }()
    
    // 登录注册按钮点击事件
    func logInButtonClick (){
        // 实现闭包的回调
        visitorCloseure?()
        
    }
    

}



















